﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageSystem.Dialog
{
    public partial class DialogOrder : Form
    {
        private TextBox _orderSnText = null;
        private YCD_ORDER _orderW = null;
        private List<YCD_ORDER> _listOrder = null;

        public DialogOrder(TextBox orderText, YCD_ORDER orderW=null)
        {
            _orderSnText = orderText;
            _orderW = orderW;
            InitializeComponent();
        }

        //设置listview
        PRODUCT_SN proSnW = new PRODUCT_SN();//计算SN数量
        private void SetListView(ListView view, YCD_ORDER order)
        {
            if (view == null || order == null) return;
            ListViewItem item = new ListViewItem();
            item.Text = order.ORDER_SN;

            item.SubItems.Add(order.PRODUCT_CODE);


            item.SubItems.Add(order.PROVINCE);


            item.SubItems.Add(order.DEVICE_TYPE);

            item.SubItems.Add(order.NUMBER.ToString());
            //增加未分配SN数量
            proSnW.Clear();
            proSnW.SetWhere(string.Format("(LINE_ID is null OR LINE_ID='') AND ORDER_SN='{0}'",order.ORDER_SN));
            var selectProSnCount = MethodWcf.SelectProductSnCountClient(proSnW);
            if (selectProSnCount.result)
            {
                item.SubItems.Add(selectProSnCount.message);
            }

            view.Items.Add(item);
        }
        //清空list view
        private void ClearListView(ListView view)
        {
            view.Items.Clear();
        }
        //加载产线信息
        private void DialogOrder_Load(object sender, EventArgs e)
        {
            ClearListView(this.listViewOrder);

            if (_orderW == null)
            {
                _orderW = new YCD_ORDER();
                _orderW.SetWhere();
                _orderW.FINISHED = false;
                _orderW.DROPED = false;
            }

            var selectOrder = MethodWcf.SelectOrderClient(_orderW);
            if (selectOrder.result)
            {
                _listOrder = (from o in selectOrder.value
                              orderby o.ID descending
                              select o).ToList();

                for (int i = 0; i < _listOrder.Count; i++)
                {
                    SetListView(this.listViewOrder, _listOrder[i]);
                }
            }
        }
        //选择产线
        private void listViewOrder_MouseClick(object sender, MouseEventArgs e)
        {
            var selectOrder = this.listViewOrder.SelectedItems[0].Text;
            var orderRow = (from o in _listOrder
                           where o.ORDER_SN == selectOrder
                           select o).FirstOrDefault();

            if (orderRow == null)
            {
                MessageBox.Show("未能找到选择的产线");
                Close();
                return;
            }

            this._orderSnText.Text = orderRow.ORDER_SN;
            Close();
        }
    }
}
